﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheDrinksApp.Models;
using TheDrinksApp.Database;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace TheDrinksApp.Screen.CurrentOrder
{
    public partial class OrderItemDetail : UserControl
    {
        private int count = 0;
        private string sizeName;
        public int Count
        {
            get { return count; }
            set
            {
                if (value >= 0)
                {
                    count = value;
                    tbCount.Text = count.ToString();
                }
            }
        }
        public MainPage parent;
        public OrderItemDetail(MainPage _parent)
        {
            InitializeComponent();
            parent = _parent;
        }
        private bool isCheckIce;
        private bool isCheckLemon;

        public bool IsCheckLemon
        {
            get { return isCheckLemon; }
            set { isCheckLemon = value; }
        }

        public bool IsCheckIce
        {
            get { return isCheckIce; }
            set { isCheckIce = value; }
        }


        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            if (Util.nowEditting != null)
            {
                lbName.Text = Util.nowEditting.drink.DrinkName.ToUpper();
                sizeName = Util.nowEditting.size.SizeName.ToUpper();
                Count = Util.nowEditting.quantity;
                
                    if (Util.nowEditting.category.Catergory.ToUpper().Equals(GlobalVariable.COCKTAILS))
                    {
                        if (Util.nowEditting.drink.Description != null)
                        {
                            description.Text = Util.nowEditting.drink.Description;
                            description.Visibility = Visibility.Visible;
                        }
                        else
                            description.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        description.Visibility = Visibility.Collapsed;
                    }
                    if (!Util.nowEditting.category.Catergory.ToUpper().Equals(GlobalVariable.SOFT_DRINKS) && !Util.nowEditting.category.Catergory.ToUpper().Equals(GlobalVariable.SPIRITS))
                    {
                        layoutYesNo.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        layoutYesNo.Visibility = Visibility.Visible;
                    }
                    IsCheckIce = Util.nowEditting.ice;
                    IsCheckLemon = Util.nowEditting.lemon;
                    tbSizeName.Text = sizeName;
                    string imageUrl = (IsCheckIce ? @"/Resources/on.png" : @"/Resources/off.png");
                    ImageSourceConverter converter = new ImageSourceConverter();
                    isIceImage.Source = (ImageSource)converter.ConvertFromString(imageUrl);
                    imageUrl = (IsCheckLemon ? @"/Resources/on.png" : @"/Resources/off.png");
                    converter = new ImageSourceConverter();
                    isLemonImage.Source = (ImageSource)converter.ConvertFromString(imageUrl);
            }
        }

        private void btnPlus_Click(object sender, RoutedEventArgs e)
        {
            if (Count < 99)
                Count++;
        }

        private void btnMinus_Click(object sender, RoutedEventArgs e)
        {
            if (Count > 0)
                Count--;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (Util.nowEditting != null)
            {
                 foreach (CurrentOrderDAO order in Util.currentOrders)
                    {
                        if (order.drink == Util.nowEditting.drink && order.size == Util.nowEditting.size )
                        {
                            order.quantity = Count;
                            order.ice = IsCheckIce;
                            order.lemon = IsCheckLemon;
                            Util.RefreshListCurrentOrders();
                            break;
                        }
                        
                    }
            }
            GlobalVariable.mainPage.ShowPage(Util.preDetailPage);
        }
        private void isIce_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            IsCheckIce = !IsCheckIce;
            string imageUrl = (IsCheckIce ? @"/Resources/on.png" : @"/Resources/off.png");
            ImageSourceConverter converter = new ImageSourceConverter();
            isIceImage.Source = (ImageSource)converter.ConvertFromString(imageUrl);
        }

        private void isLemon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            IsCheckLemon = !IsCheckLemon;
            string imageUrl = (IsCheckLemon ? @"/Resources/on.png" : @"/Resources/off.png");
            ImageSourceConverter converter = new ImageSourceConverter();
            isLemonImage.Source = (ImageSource)converter.ConvertFromString(imageUrl);
            
        }
    }
}
